<?php

// no direct access
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

/**
 * Joomla! Full Page Background Plugin
 *
 * @package		Joomla.Plugin
 * @subpackage	System.fullpagebackground
 *
 * 	Copyright (C) 2011  Brian Tyndall - http://www.webdogz.co.uk
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class plgSystemFullPageBackground extends JPlugin
{
	
		function onAfterInitialise()
		{
			$app = JFactory::getApplication();
	
			if ($app->isSite()) {
				
				require_once(dirname(__FILE__) . DS . 'includes' . DS . 'functions.php');
				if($this->params->get("disableMobileDevices") == 1) {
					require_once(dirname(__FILE__) . DS . 'includes' . DS . 'mobileDetect.php');
				}
				
				if($this->params->get("disableMobileDevices") == 1 && detect_mobile() == false || $this->params->get("disableMobileDevices") == 0){
						$doc =& JFactory::getDocument();
						$doc->addScript('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js');
						$doc->addScript('plugins' . DS . 'system' . DS . 'fullpagebackground' . DS . 'js' . DS . 'supersized.3.0.core.js');
						$doc->addStyleSheet('plugins' . DS . 'system' . DS . 'fullpagebackground' . DS . 'css' . DS . 'supersized.css');
						$baseURI = JURI::base();			
						if($this->params->get("randomImage") == 1) {
							$imagesPath = JPATH_BASE . '/images/' . $this->params->get("imagesFolder") . '';
							$imagesPath = fixPath($imagesPath);
							if(checkEmptyDirectory($imagesPath)) {
								$my_images = listImages($imagesPath, $this->params->get("minimumImageSize"), $this->params->get("minimumWidth"), $this->params->get("minimumHeight"));
								if(!(empty($my_images))) {
								$doc->addScriptDeclaration(assignImage($baseURI . implode(randomImage($my_images)), $this->params->get("displayCenter")));
								} else { JFactory::getApplication()->enqueueMessage( JText::_( 'Background Image Folder is Empty!' ), 'error' );}
							} else {
								JFactory::getApplication()->enqueueMessage( JText::_( 'Background Image Folder is Empty!' ), 'error' );
							}
							
						} else {
							$menuItemID = JRequest::getInt('Itemid');
							$menu = &JSite::getMenu();
							$defaultMenu = '';
							$defaultMenu = parseObjectToArray($menu->getDefault());	
							if($menuItemID == 0) {
								$menuItemID = (int)$defaultMenu["id"];
							}
							$imagelink = '';
							$image1src = $this->params->get("image1src");
							$image2src = $this->params->get("image2src");
							$image3src = $this->params->get("image3src");
							$image4src = $this->params->get("image4src");
							$image5src = $this->params->get("image5src");
							$image6src = $this->params->get("image6src");
							$image7src = $this->params->get("image7src");
							$image8src = $this->params->get("image8src");
							$image9src = $this->params->get("image9src");
							$image10src = $this->params->get("image10src");
							$imagedefaultsrc = $this->params->get("imagedefaultsrc");
							if(!(empty($image1src)) && $this->params->get("image1menu") == $menuItemID)  {
									$imagelink = $image1src;
							} elseif (!(empty($image2src)) && $this->params->get("image2menu") == $menuItemID) {
									$imagelink = $image2src;
							} elseif (!(empty($image3src)) && $this->params->get("image3menu") == $menuItemID) {
									$imagelink = $image3src;
							} elseif (!(empty($image4src)) && $this->params->get("image4menu") == $menuItemID) {
									$imagelink = $image4src;
							} elseif (!(empty($image5src)) && $this->params->get("image5menu") == $menuItemID) {
									$imagelink = $image5src;
							} elseif (!(empty($image6src)) && $this->params->get("image6menu") == $menuItemID) {
									$imagelink = $image6src;
							} elseif (!(empty($image7src)) && $this->params->get("image7menu") == $menuItemID) {
									$imagelink = $image7src;
							} elseif (!(empty($image8src)) && $this->params->get("image8menu") == $menuItemID) {
									$imagelink = $image8src;
							} elseif (!(empty($image9src)) && $this->params->get("image9menu") == $menuItemID) {
									$imagelink = $image9src;
							} elseif (!(empty($image10src)) && $this->params->get("image10menu") == $menuItemID) {
									$imagelink = $image10src;
							} elseif (!(empty($imagedefaultsrc))) {
									$imagelink = $imagedefaultsrc;
							}
							if(!empty($imagelink)) {
								$doc->addScriptDeclaration(assignImage($baseURI . $imagelink, $this->params->get("displayCenter")));
							}
						
						}			
					}		
		
			}
	
		}
	
	
	
}

?>